﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dss.Data;

namespace Dss.Methods
{
    public class ProjectActivities
    {
        public ProjectActivities()
        {
            cecbContext = new DSSEntities();
        }
        DSSEntities cecbContext;

        //// All the project names are returned
        public List<Project> getAllProjectNames()
        {
            IQueryable<Project> Query =
            from c in cecbContext.Projects
            select c;

            List<Project> ProjectNames = Query.ToList();

            return ProjectNames;
        }

        //Project reference is passed as a parameter and the project category is returned
        public String getAllProjectCatByReference(String reference)
        {
            var Cat = (from c in cecbContext.Projects
                       where c.Project_Reference == reference
                       select c).First();

            String projectCat = Cat.Project_Catagory.ToString();

            return projectCat;

            
        }

        // Project activities are inserted
        public bool InsertProjectActivity(String name, String phase)
        {
            bool IsInserted = false;
            try
            {
                String reference = getNewActivityReference();
                Activity actvty = new Activity();
                actvty.Actv_Reference = reference;
                actvty.Actv_Name = name;
                actvty.Actv_Phase = phase;
                cecbContext.AddToActivities(actvty);
                cecbContext.SaveChanges();

                 

                IsInserted = true;
            }
            catch (Exception)
            {

                IsInserted = false;
            }


            return IsInserted;
        }

        //generate new ActivityReference
        public String getNewActivityReference()
        {

            var idKey = (from c in cecbContext.Activities
                         orderby c.Actv_Id descending
                         select c).First();

            String id = idKey.Actv_Reference.ToString();
            int sub = int.Parse(id.Substring(3)) + 1;
            String newId = "Atv" + sub;
            return newId.ToString();
        }
    }
}
